package d1_com.itheima.thread;

import java.util.ArrayList;
import java.util.List;

public class Desk {
    private List<String> list = new ArrayList<>();

    public synchronized void put() {
        String name = Thread.currentThread().getName();
        try {
            if (list.size() == 0) {
                list.add(name + "做的包子");
                System.out.println(name + "做了一个包子~~");
                Thread.sleep(1000);
                //唤醒别人，等待自己
                this.notifyAll();
                this.wait();
            } else {
                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public synchronized void get() {
        String name = Thread.currentThread().getName();
        try {
            if (list.size() == 1) {
                System.out.println(name + "吃了" + list.get(0));
                list.clear();
                Thread.sleep(500);
                //唤醒别人，等待自己
                this.notifyAll();
                this.wait();
            } else {
                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
